<template>
  <div class="home">
    <div class="content">
      <div class="left">
        <div class="GoodSelect">
          <div class="GoodSelectTitle">商品类别</div>
          <el-radio-group
            class="GoodSelectGroud"
            v-model="radio1"
            fill="#409EFF"
            @change="SelectGoods"
          >
            <div class="radio" v-for="(item, index) in category" :key="index">
              <el-radio :label="item.index"
                ><span class="radioText">{{ item.title }}</span>
              </el-radio>
            </div>
            <!-- <div class="radio">
              <el-radio :label="1"
                ><span class="radioText">虚拟商品</span>
                <span class="radioNum">25 </span>
              </el-radio>
            </div>
            <div class="radio">
              <el-radio :label="2"
                ><span class="radioText">实体商品</span>
                <span class="radioNum">56 </span>
              </el-radio>
            </div> -->
          </el-radio-group>
        </div>

        <div class="GoodSelect">
          <div class="GoodSelectTitle">商品标签</div>
          <el-radio-group
            class="GoodSelectGroud"
            v-model="radio2"
            fill="#409EFF"
            @change="SelectLabel"
          >
            <div
              class="radio"
              v-for="(item, index) in selsectLabel"
              :key="index"
            >
              <el-radio :label="item.categoryindex + '-' + item.index"
                ><span class="radioText">{{ item.title }}</span>
              </el-radio>
            </div>
            <!-- <div class="radio">
              <el-radio :label="1"
                ><span class="radioText">游戏周边</span>
                <span class="radioNum">25 </span>
              </el-radio>
            </div>
            <div class="radio">
              <el-radio :label="2"
                ><span class="radioText">电子产品</span>
                <span class="radioNum">56 </span>
              </el-radio>
            </div> -->
          </el-radio-group>
        </div>
      </div>

      <div class="right">
        <div class="Goods">
          <div class="GoodsTitle">
            商品专区
          </div>
          <el-switch
            v-model="value1"
            active-text="可兑换"
            inactive-text="全部"
            style="margin:20px 0 0 0"
          >
          </el-switch>
          <noMore v-if="commodityData.length == 0"></noMore>

          <div class="GoodsList">
            <div
              class="GoodsBox"
              v-for="(item, index) in commodityData"
              :key="index"
              @click="navigationSelect(item._id)"
            >
              <div class="GoodsImg">
                <img class="GoodsImg" :src="item.mainimg[0] | imgF" />
              </div>
              <div class="GoodsBottom">
                <div class="GoodsBottomLeft">
                  <div class="GoodsName">{{ item.title }}</div>
                  <div class="GoodsNum">{{ item.integral }}积分</div>
                </div>
                <div class="GoodsBottomRight">
                  <el-button
                    type="text"
                    class="GoodsBtn"
                    @click="navigationSelect(item._id)"
                    >可兑换</el-button
                  >
                </div>
              </div>
            </div>
          </div>
          <el-pagination
            background
            @current-change="handleCurrentChange"
            :page-size="limitSize"
            layout="prev, pager, next, jumper"
            :total="RecordeSize"
          ></el-pagination>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { get_commodity } from "../../api/commodity";
import { get_label } from "../../api/label";
import { get_category } from "../../api/category";
import { get_user_info } from "../../api/user_login";
import { commodity_img_prefix } from "../../utils/global";
import noMore from "@/components/noMore";

export default {
  name: "IntegralMall",
  components: {
    noMore
  },
  data() {
    return {
      activeName: "second",
      radio1: "0",
      radio2: "0-0",
      category: [], //商品类别
      label: [], //标签
      selsectLabel: [], //筛选标签
      commodityData: [], //
      reqdatas: {},
      skipSize: 0, //开始位置
      RecordeSize: 0, //总文章
      limitSize: 12,
      value1: false //用户切换可兑换
    };
  },
  filters: {
    imgF(val) {
      return commodity_img_prefix + val;
    }
  },
  watch: {
    async value1(val) {
      console.log(val);
      var user = await get_user_info({
        query: { _id: this.$store.getters.userInfo._id }
      });
      if (val) {
        this.reqdatas.integral = {
          $lte: parseInt(user.data[0].integral)
        };
      } else {
        delete this.reqdatas.integral;
      }
      this.getGoods();
    }
  },
  async created() {
    this.getGoods();
    var label = await get_label({ sort: { index: "1" } });
    console.log("label", label);
    this.label = label.data;
    this.selsectLabel = label.data;
    var categorydata = await get_category({
      sort: { categoryindex: "1", index: "1" }
    });
    console.log("categorydata", categorydata);
    this.category = categorydata.data;
  },
  methods: {
    SelectGoods(e) {
      this.reqdatas.categoryindex = e;
      this.radio2 = "0-0";
      delete this.reqdatas.labelindex;
      if (e == 0) {
        console.log("全部标签");
        //全部标签
        delete this.reqdatas.categoryindex;
        this.selsectLabel = this.label;
      } else {
        var arr = this.label;
        this.selsectLabel = [];
        for (var i = 0; i < arr.length; i++) {
          var ai = arr[i];
          if (ai.categoryindex == e || ai.categoryindex == 0) {
            this.selsectLabel.push(ai);
          }
        }
      }
      this.getGoods();
    },
    SelectLabel(e) {
      if (e == "0-0") {
        delete this.reqdatas.labelindex;
        delete this.reqdatas.categoryindex;
      } else {
        var categoryindex = e.match(/(\S*)-/)[1];
        var labelindex = e.match(/-(\S*)/)[1];
        this.reqdatas.labelindex = labelindex;
        this.reqdatas.categoryindex = categoryindex;
      }
      this.getGoods();
    },
    async getGoods() {
      let query = {
        query: {
          ...this.reqdatas
        },
        sort: { _id: -1 },
        skip: this.skipSize,
        limit: this.limitSize,
        count: true
      };
      console.log(query, "sddsds");
      var commodityData = await get_commodity(query);
      console.log(commodityData);
      this.commodityData = commodityData.data;
      this.RecordeSize = commodityData.countnum;
    },
    navigationSelect(_id) {
      console.log(_id);
      this.$router.push({
        path: "/productDetails", //跳转路径
        name: "productDetails", //跳转路径的name值，不写跳转后页面取不到参数
        // 参数
        query: {
          _id: _id
        }
      });
    },
    handleCurrentChange(val) {
      this.skipSize = (val - 1) * this.limitSize;
      console.log(`当前页: ${val}`);
      this.getGoods();
    }
  }
};
</script>

<style lang="scss" scoped>
.home {
  width: 100%;
}
.content {
  display: flex;
  width: 100%;
  justify-content: space-between;
  margin-top: 20px;
  .left {
    background-color: #ffffff;
    min-height: 600px;
    width: 25%;
    display: flex;
    flex-direction: column;
    align-items: center;
    .GoodSelect {
      width: 80%;
      display: flex;
      flex-direction: column;
      margin: 20px 0 0 0;
      align-items: flex-start;
      .GoodSelectTitle {
        width: 100%;
        font-size: 18px;
        padding: 20px 0 10px 0;
        border-bottom: solid 1px #cac9c9;
      }
      .GoodSelectGroud {
        margin: 30px 0 0 0;
        width: 100%;
        display: flex;
        flex-direction: column;
        height: auto;
        justify-content: space-between;
        font-size: 42px;
        .radio {
          width: 100%;
          font-size: 20px;
          height: 48px;
          display: flex;
          align-items: center;
          flex-direction: row;
          justify-content: space-between;
          /deep/ {
            .el-radio {
              margin-bottom: px(20);
              //从这里开始 就是更改颜色的代码，将你的颜色           改掉我的颜色就可以了
              .el-radio__input {
                margin-bottom: px(5);
                &.is-checked {
                  .el-radio__inner {
                    background-color: #012fa3;
                    border-color: #012fa3;
                    color: #012fa3;
                  }
                }
                .el-radio__inner {
                  &:hover {
                    border-color: #012fa3;
                  }
                }
              }
              .el-radio__input.is-checked + .el-radio__label {
                color: #012fa3 !important;
              }
              //到这里结束
              .el-radio__label {
                font-size: 16px;
                font-family: SourceHanSansCN-Normal, SourceHanSansCN;
                font-weight: 400;
                color: #a6a6a6;
                line-height: px(40);
                padding: 0 0 0 px(10);
              }
            }
          }
          .radioText {
            font-size: 18px;
          }
          .radioNum {
            font-size: 18px;
            margin: 0 0 0 60px;
          }
        }
      }
    }
  }

  .right {
    width: 74%;
    background: #ffffff;
    min-height: 600px;
    .Goods {
      width: 90%;
      margin: 50px auto;
      .GoodsTitle {
        font-size: 18px;
        border-bottom: 1px solid #a6a6a6;
        padding: 14px 0;
      }
      .GoodsList {
        display: flex;
        flex-wrap: wrap;
        .GoodsBox {
          cursor: pointer; /*鼠标悬停变小手*/
          border: #cac9c9 1px solid;
          margin: 20px;
          .GoodsImg {
            width: 175px;
            height: 170px;
            background-color: #f6f7f7;
          }
          .GoodsBottom {
            width: 175px;
            height: 60px;
            display: flex;
            justify-content: space-between;
            .GoodsBottomLeft {
              margin: 0 0 0 10px;
              display: flex;
              flex-direction: column;
              justify-content: space-around;
              .GoodsName {
                font-size: 15px;
                width: 100px;
                height: 40px;
                line-height: 20px;
                white-space: nowrap;
                text-overflow: ellipsis;
                overflow: hidden;
              }
              .GoodsNum {
                color: #d43030;
                font-size: 14px;
              }
            }
            .GoodsBottomRight {
              margin: 0 10px 0 0;
              .GoodsBtn {
                color: #ff8d1a;
              }
            }
          }
        }
      }
    }
  }
}
</style>
